<?php 
App::uses('AppController', 'Controller');

class StallsAddController extends AppController {
	var $components = array('Link','Files','Strings');
	public $user = "";
	public function beforeFilter(){
		$this->user = parent::checkMemberLogin();
		$this->set("user",$this->user);
	}
	/**
	 * thêm gian hàng
	 * @author:QuocTran
	 */
	public function index() {
		$this->loadModel('RootCategory');
		$this->loadModel('Shop');
		$this->loadModel('ShopLocation');
		$this -> layout = 'layoutNoMenuLeft';
		$this->set('title_for_layout','Tạo gian hàng');
		$shop = $this->Shop->findShopByCreatedId($this->user['id']);
		
		$this->set('rootCategories',$this -> RootCategory->findAllData());
		// moi user chỉ tạo dc 1 gian hàng
		if(!empty($shop)){
			$this->redirect("/quan-ly-user");
		}
		
		if ($this -> request -> is('post')) {
			$new = $this -> data;
			
			$new['banner_image'] = $this->Files->uploadFile(UPLOAD_IMG_SHOP, $new['banner_image']['tmp_name'], ROOT_IMG_SHOP, $new['banner_image']['name']);
			$new['logo_image'] = $this->Files->uploadFile(UPLOAD_IMG_SHOP, $new['logo_image']['tmp_name'], ROOT_IMG_SHOP, $new['logo_image']['name']);
			$new['short_url'] = $this->Link->getShortLink($new['shop_name']);
			$new['shop_name'] = $this -> Strings -> replaceHTMLTag($new['shop_name'], ",");
			$new['deleted'] = 0;
			$new['created_by'] = $this->user['id'];//lấy id của user login
			$new['created_at'] = date("Y-m-d H:i:s");
			$new['updated_by'] = $this->user['id'];//lấy id của user login
			$new['updated_at'] = date("Y-m-d H:i:s");
			
			if($this->Shop->save($new)){
				$idShop = $this->Shop->id;
				
				$shopLocation = $new['shopLocation'];
				$shopLocationNew = array();
				for($i=0; $i<3; $i++) {
					if($shopLocation[$i]['phone'] != ""){
						$shopLocation[$i]['shop_id'] = $idShop;
						$shopLocation[$i]['deleted'] = 0;
						$shopLocation[$i]['created_by'] = $this->user['id'];
						$shopLocation[$i]['created_at'] = date("Y-m-d H:i:s");
						$shopLocation[$i]['updated_by'] = $this->user['id'];
						$shopLocation[$i]['updated_at'] = date("Y-m-d H:i:s");
						array_push($shopLocationNew, $shopLocation[$i]);
					}
				}
				if(count($shopLocationNew) > 0){
					$this->ShopLocation->saveMany($shopLocationNew);
				}
			}
			$shopId = $this->Shop->id;
			$this->redirect("/ShopCategories");
			
		}
	}
	/**
	 * sửa gian hàng
	 * @author:QuocTran
	 */
	public function edit() {
		$this -> layout = 'layoutNoMenuLeft';
		$this->loadModel('Shop');
		$this->loadModel('RootCategory');
		$this->loadModel('ShopLocation');
		
		$this->set("action","/StallsAdd/edit");
		
		$this->set('title_for_layout','Chỉnh sửa gian hàng');
		
		$this->set('rootCategories',$this -> RootCategory->findAllData());
		$conditions = array("Shop.created_by" => $this->user['id'], "Shop.deleted" =>0);
		$data = $this->Shop->find('first', array('conditions' => $conditions));
		if(empty($data)){
			$this->redirect("/quan-ly-user");
		}
		$shopId = $data['Shop']['id'];
		$shopLocation = $this->ShopLocation->find('all',array(
			'conditions' => array('shop_id' => $data['Shop']['id'], "deleted"=>0),
			'order' => array('location_id' => 'asc')
			));
		$this->set("shopLocation",$shopLocation);
		
		$n = count($shopLocation);
		$arrLocationId = array();
		for($i = 0; $i < $n; $i++){
			array_push($arrLocationId, $shopLocation[$i]["ShopLocation"]["location_id"]);
		}
		$arrLocation = array(0,1,2);
		$arr =  array();
		// lấy danh sách khu vuc chưa dang ký truoc đó
		for($j = 0; $j < count($arrLocation); $j++){
			$r = array_search($arrLocation[$j], $arrLocationId);
			if(is_int($r) == false){
				array_push($arr, $arrLocation[$j]);
			}
		}
		$this->set("locationNew", $arr);
		$this -> set("data", $data['Shop']);
		
		if ($this -> request -> is('post')) {
			
			$update = $this -> data;
			
			/*echo "<pre>";
			print_r($update);
			exit;*/
			if($update['banner_image']['name'] != ""){
				$update['banner_image'] = $this->Files->uploadFile(UPLOAD_IMG_SHOP, $update['banner_image']['tmp_name'], ROOT_IMG_SHOP, $update['banner_image']['name']);
				$this->Files->deleteFile(WWW_ROOT.$update['banner_image_old']);
			}else{
				unset($update['banner_image']);
			}
			
			if($update['logo_image']['name'] != ""){
				$update['logo_image'] = $this->Files->uploadFile(UPLOAD_IMG_SHOP, $update['logo_image']['tmp_name'], ROOT_IMG_SHOP, $update['logo_image']['name']);
				$this->Files->deleteFile(WWW_ROOT.$update['logo_image_old']);
			}else{
				unset($update['logo_image']);
			}
			
			$update['updated_by'] = $this->user['id'];//lấy id của user login
			$update['updated_at'] = date("Y-m-d H:i:s"); 
			
			$this->Shop->save($update);
			
			//shop location
			$shopLocation = $update['shopLocation'];
			$shopLocationUpdate = array();
			for($i=0; $i<3; $i++) {
				if($shopLocation[$i]['phone'] != ""){
					$shopLocation[$i]['shop_id'] = $shopId;
					$shopLocation[$i]['deleted'] = 0;
					$shopLocation[$i]['created_by'] = $this->user['id'];
					$shopLocation[$i]['created_at'] = date("Y-m-d H:i:s");
					$shopLocation[$i]['updated_by'] = $this->user['id'];
					$shopLocation[$i]['updated_at'] = date("Y-m-d H:i:s");
					array_push($shopLocationUpdate, $shopLocation[$i]);
				}
				
				if($shopLocation[$i]['phone'] == "" && !empty($shopLocation[$i]['id'])){
					$delete = array();
					$delete['deleted'] = 1;
					$delete['id'] = $shopLocation[$i]['id'];
					
					array_push($shopLocationUpdate, $delete);
				}
			}
			if(count($shopLocationUpdate) > 0){
				$this->ShopLocation->saveMany($shopLocationUpdate);
			}
			
			$this->redirect("/quan-ly-user");
		}
	}
	/**
	 * Xóa gian hàng
	 * @author:QuocTran
	 */
	public function delete() {
		$this->loadModel('Shop');
		
		$conditions = array("Shop.created_by" => $this->user['id'], "Shop.deleted" =>0);
		$data = $this->Shop->find('first', array('conditions' => $conditions));
		parent::createException($data['Shop']);
		
		$data['Shop']['deleted'] = 1;
		$data['Shop']['updated_by'] = $this->user['id'];//lấy id của user login
		$data['Shop']['updated_at'] = date("Y-m-d H:i:s"); 
		$this -> Shop -> save($data);
		$this->redirect('/quan-ly-user');
	}	
}
?>